﻿using System;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using AccessControl.HardwareLibrary.EnumAndStruct;
using AccessControl.HardwareLibrary.Gate;


namespace ACTest.GateTest
{
    public partial class frmMain : Form
    {

        private GateCommunicator gate;
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e){
            gate = new GateCommunicator();
            gate.DataReceived += gate_DataReceived;
            gate.DataSent += gate_DataReceived;
            gate.ConnectionChanged += gate_ConnectionChanged;
            if (!gate.Connect(txtIP.Text, int.Parse(txtPort.Text))) return;

            btnDisconnect.Visible = true;
            btnConnect.Visible = false;

        }

        private void gate_ConnectionChanged(object sender, bool connected)
        {
            Invoke(
                new MethodInvoker(
                    delegate
                    {
                        txtResponse.AppendText(connected.ToString() + Environment.NewLine);
                        txtResponse.SelectionStart = txtResponse.Text.Length;
                        txtResponse.ScrollToCaret();

                    }));
        }

        private void btnSend_Click(object sender, EventArgs e){
            var rx = new Regex("\\[\\w+\\]");
            foreach (Match m in rx.Matches(txtCommand.Text))
            gate.Send(m.Value);
        }

        private void gate_DataReceived(object sender, string data){
            Invoke(
                new MethodInvoker(
                    delegate{
                            txtResponse.AppendText(data + Environment.NewLine);
                            txtResponse.SelectionStart = txtResponse.Text.Length;
                            txtResponse.ScrollToCaret();

                            if (data == "[021400197580700478]"){
                                gate.DenyGateForCard();
                                Thread.Sleep(100);
                                gate.Beep(Beeps.Triple);
                            }
                            else if (data == "[021400197580685561]")
                                gate.AllowGateForCard();

                    }));
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            gate.Disconnect();
            btnDisconnect.Visible = false;
            btnConnect.Visible = true;
        }

        private void btnClear_Click(object sender, EventArgs e){
            txtResponse.Text = "";
        }

        private void btnAllow_Click(object sender, EventArgs e)
        {
            gate.AllowGateForCard();
        }

        private void btnDeny_Click(object sender, EventArgs e)
        {
            gate.DenyGateForCard();
        }

        private void AllowCustom_Click(object sender, EventArgs e)
        {
            gate.AllowGateCustom();
        }

        private void btnEnableBuzzer_Click(object sender, EventArgs e)
        {
            gate.Send("[05011]");
        }

        private void btnDisableBuzzer_Click(object sender, EventArgs e)
        {
            gate.Send("[05010]");
        }

        private void btnSetTimeout_Click(object sender, EventArgs e)
        {
            gate.SetEntranceTimeout((int)numTimeout.Value*100);
        }

        private void btnEnableBuzzer_Click_1(object sender, EventArgs e)
        {
            gate.Send("[05011]");
        }

        private void btnDisableBuzzer_Click_1(object sender, EventArgs e)
        {
            gate.Send("[05010]");
        }

        private void btnRunBuzzer_Click(object sender, EventArgs e)
        {
            gate.Send("[07011]");
        }

        private void btnStopBuzzer_Click(object sender, EventArgs e)
        {
            gate.Send("[07010]");
        }

        private void btnBeep_Click(object sender, EventArgs e)
        {
            gate.Beep( Beeps.Triple);
        }

        private void btnSetTimeouts_Click(object sender, EventArgs e)
        {
            //gate.Send("[07011]");
            //gate.Send("[07010]");

            gate.SetServerResponseTimeout(3000);
            gate.SetEntranceTimeout(3000);
        }


    }
}
